package com.ncmmall.application.business.goods;

import com.ncmmall.querychannel.service.QueryChannelService;
import com.google.common.base.Splitter;
import com.google.common.collect.ImmutableMap;
import com.ncmmall.domain.business.goods.attribute.GoodsAttribute;
import com.ncmmall.domain.business.goods.attribute.GoodsAttributeChannel;
import com.ncmmall.domain.business.goods.attribute.GoodsAttributeChannelRelevancy;
import com.ncmmall.domain.business.goods.attribute.GoodsAttributeChannelRelevancyRepository;
import com.ncmmall.domain.business.goods.attribute.GoodsAttributeChannelRepository;
import com.ncmmall.domain.business.goods.attribute.QGoodsAttribute;
import com.ncmmall.domain.business.goods.attribute.QGoodsAttributeChannel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashSet;
import java.util.Set;

/**
 * @author linxs
 * @Description: 商品规格类目管理业务逻辑处理
 * @Date: 2019-07-09 18:30
 */
@Service
@Transactional
public class GoodsAttributeChannelApplication {

    @Autowired
    private GoodsAttributeChannelRepository attributeRepository;

    @Autowired
    private GoodsAttributeChannelRelevancyRepository relevancyRepository;

    @Autowired
    private QueryChannelService queryChannelService;

    /**
     * 添加规格
     *
     * @param attribute
     */
    public void create(GoodsAttributeChannel attribute) {
        attributeRepository.save(attribute);
    }

    /**
     * 修改规格信息
     *
     * @param params
     */
    public void modify(GoodsAttributeChannel params) {
        GoodsAttributeChannel old = queryChannelService.findOne(QGoodsAttributeChannel.goodsAttributeChannel.id.eq(params.getId()), GoodsAttributeChannel.class);
        old.setName(params.getName());
        attributeRepository.save(old);
    }


    /**
     * 删除规格信息
     */
    public void remove(GoodsAttributeChannel params) {
        attributeRepository.delete(params);
    }

    /**
     * 关联规格
     */
    public void relevancy(long attributeChannelId, String ids) {

        GoodsAttributeChannel channel = queryChannelService.findOne(QGoodsAttributeChannel.goodsAttributeChannel.id.eq(attributeChannelId), GoodsAttributeChannel.class);
        Set<String> strings = new HashSet<>(Splitter.on(";").omitEmptyStrings().trimResults().splitToList(ids));

        for (String idString : strings) {

            int count = queryChannelService.selectCount("select count(*) from mall_goods_attribute_channel_relevancy where relevancy = :cid and attribute_id = :aid",
                    ImmutableMap.of("cid", channel.getId(), "aid", Long.parseLong(idString)));
            if (count > 0) {
                continue;
            }

            GoodsAttribute attribute = queryChannelService.findOne(QGoodsAttribute.goodsAttribute.id.eq(Long.parseLong(idString)), GoodsAttribute.class);
            GoodsAttributeChannelRelevancy relevancy = new GoodsAttributeChannelRelevancy();
            relevancy.setAttribute(attribute);
            relevancy.setChannel(channel);
            relevancyRepository.save(relevancy);
        }
    }

    public void removeRelevancy(GoodsAttributeChannelRelevancy attributeChannel) {
        relevancyRepository.delete(attributeChannel);

    }
}
